package com.insigma.business.qrcode.soldier.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;

import com.insigma.business.qrcode.soldier.entity.SyfSoldierInfo;
import com.insigma.business.qrcode.soldier.service.SyfxtService;
import com.insigma.business.qrcode.soldier.util.OkHttp3Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Slf4j
@Service("syfxtService")
public class SyfxtSystemServiceImpl implements SyfxtService {

    private static Logger LOGGER = LoggerFactory.getLogger(SyfxtSystemServiceImpl.class);


    @Override
    public List<SyfSoldierInfo> querySoldierList(int pageNo, int pageSize, String cookie) {
        String urlTemplate = "http://172.18.103.246:37011/mcasc/scp/mng/listdata.do?" +
                "name=&regionCode=&cardId=&scpState=01&apptype=&birthdayStart=&birthdayEnd=" +
                "&appvDateStart=&appvDateEnd=&pq_curpage=%d&pq_rpp=%d&_=%s";
        String url = String.format(urlTemplate, pageNo, pageSize, String.valueOf(System.currentTimeMillis()));
        Map<String, String> header = Maps.newHashMap();
        header.put("Cookie", cookie);
        String s = OkHttp3Util.sendByGetUrlWithHeader(url, header);
        log.info(s);
        if (Strings.isNotBlank(s)) {
            JSONObject object = JSONObject.parseObject(s);
            JSONArray resultList = object.getJSONArray("resultList");
            if (resultList != null) {
                List<SyfSoldierInfo> syfSoldierInfos = resultList.toJavaList(SyfSoldierInfo.class);
                return syfSoldierInfos;
            }
        }

        return null;
    }
}
